
package com.application.projectview.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.application.projectview.struts.form.ProjectViewForm;
import com.manager.BaseDao;


public class ProjectViewDAO extends BaseDao {
	private static final Logger logger = Logger.getLogger(ProjectViewDAO.class);

	/**
	 * 是否完成了项目的相关步骤
	 * @return
	 */
	public ProjectViewForm findProjectViewForm(ProjectViewForm projectViewForm) {
		ProjectViewForm form = new ProjectViewForm();
		List args = new ArrayList();
		StringBuffer sql = getSQLForWorkHis(projectViewForm,args);
		List list = getListForPage(sql.toString(), args);
		if(null != list && list.size() > 0) {
			for(int i = 0; i < list.size(); i++) {
				Map map = (Map) list.get(i);
				form.setOutSourcingContract(map.get("OUTSOURCINGCONTRACT") == null || (Integer)map.get("OUTSOURCINGCONTRACT") ==0? false : true);
				form.setProgress(map.get("PROGRESS") == null || (Integer)map.get("PROGRESS") ==0? false : true);
				form.setConference(map.get("CONFERENCE") == null || (Integer)map.get("CONFERENCE") ==0? false : true);
				form.setExpense(map.get("EXPENSE") == null || (Integer)map.get("EXPENSE") ==0? false : true);
				form.setExpenseMoon(map.get("EXPENSEMOON") == null || (Integer)map.get("EXPENSEMOON") ==0? false : true);
				form.setExpenseYear(map.get("EXPENSEYEAR") == null || (Integer)map.get("EXPENSEYEAR") ==0? false : true);
				form.setMaterial(map.get("MATERIAL") == null || (Integer)map.get("MATERIAL") ==0? false : true);
				form.setEquipment(map.get("EQUIPMENT") == null || (Integer)map.get("EQUIPMENT") ==0? false : true);
				form.setAcceptance(map.get("ACCEPTANCE") == null || (Integer)map.get("ACCEPTANCE") ==0? false : true);
				form.setProjectTeams(map.get("PROJECTTEAMS") == null || (Integer)map.get("PROJECTTEAMS") ==0? false : true);
				form.setPaymentAccount(map.get("PAYMENTS") == null || (Integer)map.get("PAYMENTS") ==0? false : true);
				form.setReportMonth(map.get("REPORTMONTH") == null || (Integer)map.get("REPORTMONTH") ==0? false : true);
				form.setReportQuarter(map.get("REPORTQUARTER") == null || (Integer)map.get("REPORTQUARTER") ==0? false : true);
				form.setWorkSheetsID(projectViewForm.getWorkSheetsID());
				form.setId(projectViewForm.getId());
			}
		}
		return form;
	}
    
	/**
	 * 
	* 拼装是否完成项目相关表操作的SQL语句  
	*  
	* @param projectViewForm
	* @param args
	* @return
	 */
	private StringBuffer getSQLForWorkHis(ProjectViewForm projectViewForm,List args){
		StringBuffer sql = new StringBuffer();
		sql.append(" SELECT ");
		sql.append(" (SELECT COUNT(*)  FROM A_OUTSOURCINGCONTRACT WHERE WORKSHEETSID=?) OUTSOURCINGCONTRACT, "); //外协合同
		args.add(projectViewForm.getWorkSheetsID());
		sql.append(" (SELECT COUNT(*)  FROM A_PROGRESS WHERE WORKSHEETSID=?) PROGRESS, "); //项目进度
		args.add(projectViewForm.getWorkSheetsID());
		sql.append(" (SELECT COUNT(*)  FROM A_CONFERENCE WHERE WORKSHEETSID=?) CONFERENCE,  "); //专题会议
		args.add(projectViewForm.getWorkSheetsID());
		sql.append(" (SELECT COUNT(*)  FROM A_EXPENSE WHERE WORKSHEETSID=?) EXPENSE, "); //费用申请
		args.add(projectViewForm.getWorkSheetsID());
		sql.append(" (SELECT COUNT(*)  FROM A_EXPENSEMOON WHERE WORKSHEETSID=?) EXPENSEMOON,  "); //月费用上报
		args.add(projectViewForm.getWorkSheetsID());
		sql.append(" (SELECT COUNT(*)  FROM A_EXPENSEYEAR WHERE WORKSHEETSID=?) EXPENSEYEAR, "); //年费用上报
		args.add(projectViewForm.getWorkSheetsID());
		sql.append(" (SELECT COUNT(*)  FROM A_MATERIAL WHERE WORKSHEETSID=?) MATERIAL, "); //原料
		args.add(projectViewForm.getWorkSheetsID());
		sql.append(" (SELECT COUNT(*)  FROM A_EQUIPMENT WHERE WORKSHEETSID=?) EQUIPMENT, "); //设备
		args.add(projectViewForm.getWorkSheetsID());
	    sql.append(" (SELECT COUNT(*)  FROM A_ACCEPTANCE WHERE WORKSHEETSID=?) ACCEPTANCE, "); //验收
        args.add(projectViewForm.getWorkSheetsID());
		sql.append(" (SELECT COUNT(*)  FROM A_PROJECTTEAMS WHERE WORKSHEETSID=?) PROJECTTEAMS, "); //项目组成员
		args.add(projectViewForm.getWorkSheetsID());
		sql.append(" (SELECT COUNT(*)  FROM A_PAYMENT WHERE WORKSHEETSID=?) PAYMENTS, "); //费用支付记录
		args.add(projectViewForm.getWorkSheetsID());
		sql.append(" (SELECT COUNT(*)  FROM A_PROGRESS WHERE WORKSHEETSID=? AND REPORTTYPE='1') REPORTMONTH, "); //进度月上报
		args.add(projectViewForm.getWorkSheetsID());
		sql.append(" (SELECT COUNT(*)  FROM A_PROGRESS WHERE WORKSHEETSID=? AND REPORTTYPE='2') REPORTQUARTER "); //进度季度上报
		args.add(projectViewForm.getWorkSheetsID());
		return sql;
	}

}
